package edu.neusoft.yiqibike.controller;

import com.baomidou.mybatisplus.plugins.Page;
import edu.neusoft.yiqibike.common.annotation.LoginUser;
import edu.neusoft.yiqibike.common.cache.redis.IRedisHelper;
import edu.neusoft.yiqibike.common.constant.GlobalConstant;
import edu.neusoft.yiqibike.common.entity.mysql.Admin;
import edu.neusoft.yiqibike.common.entity.mysql.Bike;
import edu.neusoft.yiqibike.common.entity.mysql.BreakdownRecord;
import edu.neusoft.yiqibike.common.entity.vo.BikeVo;
import edu.neusoft.yiqibike.common.entity.vo.Response;
import edu.neusoft.yiqibike.common.exception.ArgumentVerifyException;
import edu.neusoft.yiqibike.common.util.MapperUtil;
import edu.neusoft.yiqibike.service.IBikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;

/**
 * User：lyc
 * Date：2018-01-26
 * Time：17:28
 * Description：单车接口
 */
@RestController
@RequestMapping("/sys/bike")
public class BikeController {
    @Autowired
    private IBikeService bikeService;
    @Autowired
    private IRedisHelper redisHelper;

    /**
     * 获取报修记录列表
     *
     * @param page 分页信息
     * @return 报修记录列表
     */
    @PostMapping("/breakdown/list")
    public Response<Page<BreakdownRecord>> list(@RequestBody Page<BreakdownRecord> page) {
        return Response.success(bikeService.getBreakdownList(page));
    }

    /**
     * 初步审核报修
     *
     * @param id     报修记录ID
     * @param status 报修记录状态
     * @param reason 驳回依据
     * @param admin  操作人员
     * @return 操作成功
     */
    @PutMapping("/breakdown/handle/{id}/{status}")
    public Response<String> handleBreakdown(@PathVariable String id, @PathVariable Integer status, @RequestBody(required = false) String reason, @LoginUser Admin admin) {
        bikeService.handleBreakdown(id, status, reason, admin);
        return Response.success();
    }

    @PutMapping("/breakdown/{bikeNo}")
    public Response<String> breakdown(@PathVariable Long bikeNo) {
        bikeService.breakdown(bikeNo);
        return Response.success();
    }

    /**
     * 获取单车列表
     *
     * @param page 分页信息
     * @return 单车列表
     */
    @PostMapping("/list")
    public Response<Page<BikeVo>> bikes(@RequestBody Page<Bike> page) {
        return Response.success(bikeService.getBikeList(page));
    }

    /**
     * 编辑单车信息
     *
     * @param bike 单车信息
     * @return 操作成功/失败
     */
    @PutMapping("/")
    public Response<String> edit(@RequestBody Bike bike) {
        return bikeService.updateBike(bike) ? Response.success() : Response.failed();
    }

    /**
     * 添加单车信息
     *
     * @param vo 单车信息
     * @return 操作成功/失败
     */
    @PostMapping("/")
    public Response<String> add(@RequestBody BikeVo vo, @LoginUser Admin admin) {
        if (Objects.isNull(vo)) {
            throw new ArgumentVerifyException("请填写单车信息");
        }
        if (Objects.isNull(vo.getBikeNo())) {
            throw new ArgumentVerifyException("单车编号不能为空");
        }
        Bike bike = MapperUtil.getInstance().map(vo, Bike.class);
        if (vo.getIsRecord() == 1) {
            bike.setRecorder(Objects.isNull(admin) ? "未知" : String.valueOf(admin.getId()));
            bike.setRecordTime(new Date());
        }
        Double[] location = new Double[2];
        location[0] = vo.getLongitude();
        location[1] = vo.getLatitude();
        return bikeService.addBike(bike, location) ? Response.success() : Response.failed();
    }

    /**
     * 生成单车编号
     *
     * @return
     */
    @GetMapping("/id")
    public Response<Long> genId() {
        return Response.success(redisHelper.incr(GlobalConstant.BIKE_NO_GEN));
    }
}


